﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Commands;
using System.Configuration;
using System.ServiceModel.Configuration;

namespace Ascent.XinKuanKong.Commands
{
    public class EnableAspNetCompatibilityCommand : Orchard.Commands.DefaultOrchardCommandHandler
    {
        [CommandName("enableaspcompatibility")]
        [CommandHelp(@"Enables Asp.Net Compatibility by modifying the site's root web.config file.")]
        public void EnableAspNetCompatibility()
        {
            Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
            ServiceModelSectionGroup serviceModelGroup = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);

            if (serviceModelGroup.ServiceHostingEnvironment.AspNetCompatibilityEnabled)
            {
                Context.Output.WriteLine("AspNetCompatibility already enabled for ServiceHostingEnvironment");
            }
            else
            {
                Context.Output.WriteLine("Enabling AspNetCompatibility for ServiceHostingEnvironment");
                serviceModelGroup.ServiceHostingEnvironment.AspNetCompatibilityEnabled = true;
            }

            if (serviceModelGroup.ServiceHostingEnvironment.MultipleSiteBindingsEnabled)
            {
                Context.Output.WriteLine("MultipleSiteBindings already enabled for ServiceHostingEnvironment");
            }
            else
            {
                Context.Output.WriteLine("Enabling MultipleSiteBindings for ServiceHostingEnvironment");
                serviceModelGroup.ServiceHostingEnvironment.MultipleSiteBindingsEnabled = true;
            }

            config.Save();
        }
    }
}